<%@page import="common.JspUtil"%>
<%@include file="/header.jsp"%>
<%@ page language="java" contentType="text/html" %>
<%@ page import="dataGateway.ItemViewTable" %>
<%@ page import="domainLogic.ItemView" %>

<%
final String loginLink = common.JspUtil.URL_CONTENT_PAGE +"/login/login.jsp";
String itemIdSt = request.getParameter("itemId");
if(itemIdSt == null)
{
	out.println(common.JspUtil.makeErrorMsg("Invalid request!", common.JspUtil.URL_DEFAULT));
	return;
}

int itemId = Integer.parseInt(itemIdSt);
ItemView selectedItem = ItemViewTable.getInstance().getItem(itemId);
if(selectedItem == null)
{
	out.println(common.JspUtil.makeErrorMsg("Invalid request!", common.JspUtil.URL_DEFAULT));
	return;
}
if(selectedItem.isBidClosed() && !common.JspUtil.isLoggedIn(session))
{	// If bid is closed, details can be seen only by logged in user
	out.println(common.JspUtil.makeErrorMsg("This bid is closed. Please login to see it", 
			loginLink));
	return;
}

String price_title;
String price_price;

if(selectedItem.getNumBids() == 0)
{
	price_title = "Starting bid: ";
	price_price = ""+selectedItem.getStartingprice();
}
else
{
	price_title = "Current bid: ";
	price_price = ""+selectedItem.getHighestPrice();
}
%>
<h3>Item details</h3>
<table border="1"  cellpadding="0" cellspacing="0" width="600" >
	<tr>
		<td width="110">category</td>
		<td width="220"><%=selectedItem.getCategoryName()%></td>
		<td rowspan="6" align="center"><img src="../pics/<%=selectedItem.getFilename()%>"></td>
	</tr>
	<tr>
		<td>seller</td>
		<td><%=selectedItem.getUserId()%></td>
	</tr>
	<tr>
		<td>Item: </td>
		<td><%=selectedItem.getName()%></td>
	</tr>
<%
if(!selectedItem.isBidClosed())
{
%>
	<tr>
		<td>Remaining time</td>
		<td><%=selectedItem.getRemainingTime()%></td>
	</tr>
	<tr>
		<td><%=price_title%></td>
		<td>$ <%=price_price%>
		<br>
		[ <%=selectedItem.getNumBids()%> bids ]
		<br>
	<%
		if(common.JspUtil.isLoggedIn(session))
			{
	%>
			<form name="frm" method="post" action="<%=common.JspUtil.URL_CONTENT_PAGE%>/bids/addBid.jsp" >
			<input type="hidden" name="itemId" value="<%=selectedItem.getId()%>">
			<input type="text" name="price" size="7" maxlength=7><input type="submit" value="Place a bid">
			<br>
			Enter $ <%=selectedItem.nextBidPrice()%> or more.
			</form>
	<%}
	else
	{%>
		<a href="<%=loginLink%>">Please login to place a bid.</a>
	<%}
	%>
		</td>
	</tr>
<%	
}
if(!selectedItem.isBidClosed() && selectedItem.getBuyoutprice() > 0)
{
%>
	<tr>
		<td>Buyout Price:</td>
		<td>$ <%=selectedItem.getBuyoutprice()%>
		<br>
	<%
	if(common.JspUtil.isLoggedIn(session))
	{%>
		<form name="frm" method="post" action="<%=common.JspUtil.URL_CONTENT_PAGE%>/bids/addBid.jsp" >
			<input type="hidden" name="itemId" value="<%=selectedItem.getId()%>">
			<input type="hidden" name="price" value=<%=selectedItem.getBuyoutprice()%>>
			<input type="submit" value="Buyout">
		</form>
	<%}
	else
	{%>
		<a href="<%=loginLink%>">Please login to buy this.</a>
	<%}
	%>
		</td>
	</tr>
<%
}
%>
<%
if(!selectedItem.isBidClosed() && selectedItem.isReservedpriceReached())
{
%>
	<tr>
		<td colspan=2>Reserved price has been reached!</td>
	</tr>	
<%
}

if(common.JspUtil.isLoggedIn(session) && selectedItem.isBidClosed())
{
%>	
	<tr>
		<td colspan=2>This bid is closed!</td>	
	</tr>
	<%
		if(selectedItem.isReservedpriceReached() && selectedItem.getNumBids()>0 )
		{
		%>
		<tr>
			<td>Winner</td>
			<td><%=selectedItem.getHighestBidder()%></td>
		</tr>			
		<tr>
			<td>Winning price</td>
			<td><%=selectedItem.getHighestPrice()%></td>
		</tr>			
		<%
		}
		else {
		%>
			<tr>
				<td colspan="2">Sorry, there is no winner!</td>	
			</tr>
			<tr>
				<td colspan="2">Good luck!</td>
			</tr> 
		<%
		}
}

// For recently viewed items cookie
common.JspUtil.addRecentItems(request, response, selectedItem);
%>


	<tr>
		<td width="110">Description</td>
		<td colspan="2"><textarea rows="4" cols="50" readonly><%=selectedItem.getDescription()%></textarea></td>
	</tr>
	
	
<%
// Only for seller page.
if(selectedItem.getUserId().equals(JspUtil.getLoggedInUserId(session)))
{
%>
	<tr>
		<td colspan = 2><hr>
		You are the seller of this Item!<br>
		Detailed information</td>
	</tr>
	<tr>
		<td>Created date</td>
		<td><%=selectedItem.getRegdate()%></td>
	</tr>	
	<tr>
		<td>Real End date</td>
		<td><%=selectedItem.getRealenddate()%></td>
	</tr>
	<tr>
		<td>Num bids</td>
		<td><%=selectedItem.getNumBids()%></td>
	</tr>
	<tr>
		<td>Starting Price</td>
		<td><%=selectedItem.getStartingprice()%></td>
	</tr>
	<tr>
		<td>Reserved Price</td>
		<td><%=selectedItem.getReserveprice()%></td>
	</tr>
	<tr>
		<td>Buyout Price</td>
		<td><%=selectedItem.getBuyoutprice()%></td>
	</tr>
	<tr>
		<td>Highest Price</td>
		<td><%=selectedItem.getHighestPrice()%></td>
	</tr>
<%
}
%>
</table>

<%
// Only for seller page.
if(selectedItem.getUserId().equals(JspUtil.getLoggedInUserId(session)))
{
%>
	<jsp:include page="/bids/viewBidsList.jsp"/>
<%
}
%>
